#!/usr/bin/python
# coding=utf8

from database import Storage
import time, sys

storage = Storage()
f = open("db.txt", 'r')

i = 0
for x in xrange(i):
    f.readline()

#print f.readline()
#sys.exit()

def r(fr):
    global i
    i += 1
    res = unicode(fr.readline()[0:-2], 'cp1251')
    if len(res) == 0:
        i += 1
        res = unicode(fr.readline()[0:-2], 'cp1251')
    print "READLINE:", i, ":", res
    if res[-1] == '"':
        return  res[0:-1] if res[0] != '"' else res[1:-1]
    else:
        res += r(fr)
    return res

i = 0
j = 0
while True:
    j += 1
    print "RECORD #", j
    b = r(f)
    if b == '':
        break
    print "DATE:", b
    adate = time.mktime((int(b[6:10]), int(b[3:5]), int(b[0:2]), 0, 0, 0, 0, 0, -3))
    aclient = r(f)
    aaddress = r(f)
    amodel = r(f)
    adisign = r(f)
    try:
        awarranty = int(r(f))
    except:
        awarranty = 0
    aserial_number = r(f)
    ainventory_number = r(f)
    comment1 = r(f)
    attn = [0]
    aschet = [0]
    comment2 = r(f)
    acode = 0
    comment3 = r(f)
    acomment = u""
    print adate, aclient, aaddress, amodel, adisign, awarranty, attn, aschet, \
        acode, acomment, comment1, comment2, comment3
    print storage.save_main(adate, aclient, aaddress, amodel, adisign, awarranty,
        attn, aschet, acode, acomment, [{'serial_number': aserial_number, 
        'inventory_number': ainventory_number}], comment1, comment2, comment3)
f.close()
# 596739